<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台函数文件
// +----------------------------------------------------------------------
use think\Db;
function getUserField()
{
    return $userField = [
        ['field'=>'username','title'=>'账号','msg'=>'用户信息','type'=>2],
        ['field'=>'nickname','title'=>'姓名','msg'=>'用户信息','type'=>2],
        ['field'=>'mobile','title'=>'手机号','msg'=>'用户信息','type'=>2],
        ['field'=>'rolename','title'=>'角色名','msg'=>'用户信息','type'=>2],
    ];
}
function getProductField()
{
    $userField = getUserField();
    $productField = [
        ['field'=>'nums','title'=>'产品编号','msg'=>'产品','type'=>2],
        ['field'=>'title','title'=>'产品名称','msg'=>'产品','type'=>2],
        ['field'=>'danwei','title'=>'库存单位','msg'=>'产品','type'=>2],
        ['field'=>'guige','title'=>'产品规格','msg'=>'产品','type'=>2],
        ['field'=>'addtime','title'=>'创建时间','msg'=>'产品','type'=>2],
        ['field'=>'admin_id','title'=>'创建人','msg'=>'产品','type'=>2],
        ['field'=>'mdtime','title'=>'最后更新时间','msg'=>'产品','type'=>2],
    ];
    $productField = array_merge($productField,getExtraField('product'));
    return ['user'=>$userField,'now'=>$productField];
}

function getExtraField($type)
{
    $typeArr = [
        'product'=>'产品'
    ];
    $extra = Db::name('gd_custom')->where('type',$type)->select();
    $data = [];
    foreach($extra as $k=>$row){
        $data[] = ['field'=>'extra_'.($k+1),'title'=>$row['title'],'msg'=>$typeArr[$type],'type'=>2];
    }
    return $data;
}

//转译隐藏条件
function exchangeHidden($hidden,$type)
{
    if($type == 'product'){
        $field = getProductField();
    }
    $fieldUser = [];
    $fieldSelf = [];
    foreach($field['user'] as $k=>$row){
        $fieldUser[$row['title']] = $row['field'];
    }
    foreach($field['now'] as $k=>$row){
        $fieldSelf[$row['title']] = $row['field'];
    }
    $hidden = html_entity_decode($hidden);
    preg_match_all("/(?<={)[^}]+/",$hidden,$match);
    foreach($match[0] as $m){
        $condition = explode('.',$m);
        if($condition[0] == '用户信息'){
            $field = $fieldUser[$condition[1]];
        }else{
            $field = $fieldSelf[$condition[1]];
        }
        $hidden = str_replace($m,$field,$hidden);
    }
    $hidden = str_replace("'",'\"',$hidden);
    return $hidden;
}

/**
 * 处理时间(将 秒数 转为 时分秒 )
 * 
 * @param string $total_time 秒数
 * @return string $ftime 处理好的时间
 */
function ftime($total_time)
{
    $s = gmdate('H:i:s', $total_time);
    // dump($s);die;
    $time = explode(':', $s);
    $time['0'] == '00' ? $h = 0 : $h = $time['0'];
    $time['1'] == '00' ? $m = 0 : $m = $time['1'];
    $time['2'] == '00' ? $s = 0 : $s = $time['2'];
    $ftime = (
        empty($h) ? (
            empty($m) ? $s .'秒' : (
                empty($s) ? $m . '分' : $m . '分' . $s .'秒'
                )
            ) :(
            empty($m) && empty($s) ? $h .'小时' : (
                empty($m)  ? $h . '时' . $s . '秒' : (
                    empty($s) ? $h . '小时' . $m . '分' : $h . '小时' . $m . '分' .$s . '秒'
                )
                    )
                        )
        );
    return $ftime;
}



